<!-- 组件说明 -->
<template>
  <div class="cate">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类参数</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button
            type="primary"
            @click="showAddCateDialog"
          >添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <tree-table
        class="tree-table"
        :data="catelist"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
        :show-row-hover="false"
      >
        <template
          slot="isok"
          slot-scope="scope"
        >
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted === false"
          ></i>
          <i
            class="el-icon-error"
            v-else
          ></i>
        </template>
        <template
          slot-scope="scoped"
          slot="order"
        >
          <el-tag
            type="primary"
            size="mini"
            v-if="scoped.row.cat_level === 0"
          >一级</el-tag>
          <el-tag
            type="success"
            size="mini"
            v-if="scoped.row.cat_level === 1"
          >二级</el-tag>
          <el-tag
            type="warning"
            size="mini"
            v-if="scoped.row.cat_level === 2"
          >三级</el-tag>
        </template>
        <template slot="option">
          <el-button type="primary">编辑</el-button>
          <el-button type="danger">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 5, 10, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加分类的对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      width="50%"
      @close="addCateDialogClosed"
    >
      <el-form
        :model="addCateForm"
        :rules="addCateFormRules"
        ref="addCateFormRef"
        label-width="100px"
      >
        <el-form-item
          label="分类名称："
          prop="cat_name"
        >
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>

        <el-form-item label="父级分类：">
          <el-cascader
            v-model="selectedKeys"
            :options="parentCateList"
            :props="cascadarProps"
            @change="parentCateChanged"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="addCate"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// import x from ''
export default {
  components: {

  },
  data () {
    return {
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      catelist: [],
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'option'
        }
      ],
      addCateDialogVisible: false,
      // 添加分类表单
      addCateForm: {
        cat_name: '', // 分类名称
        cat_pid: 0, // 父级分类的id
        cat_lavel: 0 // 分类的等级，默认要添加的是1级分类
      },
      // 添加分类表单校验规则
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      // 父级分类列表数据
      parentCateList: [],
      selectedKeys: [], // 选中的属性值
      // 级联选择器配置项
      cascadarProps: {
        checkStrictly: true,
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      }
    }
  },
  mounted () {
    this.getCate()
  },
  computed: {

  },
  methods: {
    // 获取商品分类
    async getCate () {
      const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.console.error('获取商品分类失败')
      }
      console.log(res.data)
      this.catelist = res.data.result
      this.total = res.data.total
    },
    // 监听pageSize事件的改变
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getCate()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getCate()
    },
    // 添加分类对话框
    showAddCateDialog () {
      this.addCateDialogVisible = true
      this.getParentCateList()
    },
    // 获取父级分类的属性列表
    async getParentCateList () {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取父级分类数据失败！')
      }
      console.log(res.data)
      this.parentCateList = res.data
    },
    // 父级选中项改变
    parentCateChanged () {
      console.log(this.selectedKeys)
      // 选中数据后，父级分类的id发生变化
      if (this.selectedKeys.length > 0) {
        this.addCateForm.cat_pid =
          this.selectedKeys[this.selectedKeys.length - 1]
        this.addCateForm.cat_lavel = this.selectedKeys.length
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_lavel = 0
      }
    },
    // 确定添加
    addCate () {
      console.log(this.addCateForm)
      // 先对表单进行预校验
      this.$refs.addCateFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('categories', this.addCateForm)
        if (res.meta.status !== 201) {
          return this.$message.error('添加分类失败')
        }
        this.$message.success('添加分类成功')
        // 重新获取列表数据
        this.getCate()
        // 关闭弹框
        this.addCateDialogVisible = false
      })
    },
    // 关闭添加表单重置
    addCateDialogClosed () {
      this.$refs.addCateFormRef.resetFields()
      // 级联选择器清空
      this.selectedKeys = []
      // 表单绑定数据清空
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_lavel = 0
    }
  }
}
</script>

<style lang="less" scoped>
//@import url()
.el-icon-success {
  color: lightgreen;
}
.el-icon-error {
  color: red;
}
.el-cascader {
  width: 100%;
}
.el-cascader__dropdown {
  top: 300px !important;
  height: 200px;
  overflow-y: auto;
}
</style>
